//
//  GuoYaoZhunZiViewController.m
//  pocketmedicine
//
//  Created by student on 16/4/19.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "GuoYaoZhunZiViewController.h"
#import "MedicineModel.h"
#import "MedicineCell.h"
#import "AnalysisHelper.h"
#import <SVProgressHUD.h>
//#import "NetManager.h"



@interface GuoYaoZhunZiViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableViewList;

@property (weak, nonatomic) IBOutlet UITextField *numberTextField;

//@property (weak, nonatomic) IBOutlet UITextView *resultView;



@property (weak,nonatomic) NSMutableArray *medicineList;


@property (nonatomic,strong)NSString *httpArg;


@end

@implementation GuoYaoZhunZiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    NSString *httpUrl = @"http://www.tngou.net/api/drug/number";
//    NSString *httpArg = [NSString stringWithFormat:@"name=%@",self.searchBar.text];
    self.tableViewList.delegate = self;
    self.tableViewList.dataSource = self;
    self.tableViewList.hidden = YES;
  

}
- (IBAction)cancel:(id)sender {
       [self dismissViewControllerAnimated:YES completion:nil];
    [SVProgressHUD dismiss];
}
- (IBAction)btnPressed:(id)sender {
    
    [self.numberTextField resignFirstResponder];
    NSString *httpUrl = @"http://www.tngou.net/api/drug/number";
    NSString *httpArg = [NSString stringWithFormat:@"number=%@",self.numberTextField.text];
    
        [self request:httpUrl withHttpArg:httpArg];
    self.tableViewList.hidden = NO;
    
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.numberTextField resignFirstResponder];
}

-(void)request: (NSString*)httpUrl withHttpArg: (NSString*)httpArg
{
    
     [SVProgressHUD showWithStatus:@"正在搜索"];
    // 网址字符串
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, httpArg];
    // 网址
    NSURL *url = [NSURL URLWithString: urlStr];
    // 请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    // GET请求
    [request setHTTPMethod: @"POST"];
    // 设置请求头
    [request addValue: @"639a5ad1fd83cbabd5b467f9201a5c8c" forHTTPHeaderField: @"apikey"];
    
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
                                   [SVProgressHUD showWithStatus:@"搜索错误"];
                                   self.tableViewList.hidden = YES;
                               } else {
                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"HttpResponseCode:%ld", responseCode);
                                   NSLog(@"HttpResponseBody %@",responseString);
                                   
                                   NSDictionary *dict = [AnalysisHelper analysisInfo:responseString];
                                   
//                                   if (dict!=nil) {
//                                       [SVProgressHUD dismiss];}

                                 self.medicine = [MedicineModel medicineWithDict:dict];
                                   NSLog(@"%@",self.medicine);
                                   
                                   if (self.medicine.name!=nil) {
                                       self.tableViewList.hidden = NO;
                                        [SVProgressHUD dismiss];
                                   } else{
                                        [SVProgressHUD showWithStatus:@"搜索错误"];
                                       self.tableViewList.hidden = YES;
                                   }
                                   
                                   [self.tableViewList reloadData];
                                   

                                   
                                   
                            }
                           }];
    


}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MedicineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.medicine = self.medicine;
    
    
    return cell;
    
}



@end
